package com.java.test.system.feesettings;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.test.dto.PageResultDTO;
import com.java.test.dto.ResponseDTO;
import com.java.test.system.feesettings.constant.FeeSettingsCodeConst;
import com.java.test.system.feesettings.dto.FeeSettingsAddDTO;
import com.java.test.system.feesettings.dto.FeeSettingsQueryDTO;
import com.java.test.system.feesettings.dto.FeeSettingsUpdateDTO;
import com.java.test.system.feesettings.entity.FeeSettingsEntity;
import com.java.test.system.feesettings.entity.FeeTypeEntity;
import com.java.test.system.feesettings.entity.FundsTypeEntity;
import com.java.test.system.feesettings.entity.MemberLVEntity;
import com.java.test.system.feesettings.vo.FeeSettingsVO;
import com.java.test.system.feesettings.vo.FeeTypeVO;
import com.java.test.system.feesettings.vo.MemberLVVO;
import com.java.test.system.registrationtemplate.RegistrationTemplateDAO;
import com.java.test.system.registrationtemplate.entity.AreaEntity;
import com.java.test.system.registrationtemplate.entity.RegistrationTemplateEntity;
import com.java.test.util.SmartBeanUtil;
import com.java.test.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 手续费设置Service
 *
 * @author lijie1
 * @date 2020/6/30 0030
 */
@Service
public class FeeSettingsService {

    private FeeSettingsDAO feeSettingsDAO;
    @Autowired
    public void setFeeSettingsDAO(FeeSettingsDAO feeSettingsDAO) {
        this.feeSettingsDAO = feeSettingsDAO;
    }

    private RegistrationTemplateDAO registrationTemplateDAO;
    @Autowired
    public void setRegistrationTemplateDAO(RegistrationTemplateDAO registrationTemplateDAO) {
        this.registrationTemplateDAO = registrationTemplateDAO;
    }

    /**
     * 分页查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<PageResultDTO<FeeSettingsVO>> getSelectByList(FeeSettingsQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<FeeSettingsEntity> entityList = feeSettingsDAO.getSelectByList(queryDTO);
        for (FeeSettingsEntity entity:entityList) {
            String areaName = " ";
            String feeTypeName = " ";
            String fundsTypeName = " ";
            String LVName = " ";
            List<AreaEntity> areaEntityList = registrationTemplateDAO.getAreaListByIds(entity.getAreaId());
            for (AreaEntity q:areaEntityList) {
                areaName+=q.getName()+",";
            }
            entity.setAreaName(areaName.substring(0,areaName.length()-1));
            List<FeeTypeEntity> feeTypeEntityList = feeSettingsDAO.getFeeTypeListByIds(entity.getFeeType());
            for (FeeTypeEntity q:feeTypeEntityList) {
                feeTypeName+=q.getName()+",";
            }
            entity.setFeeTypeName(feeTypeName.substring(0,feeTypeName.length()-1));
            List<FundsTypeEntity> fundsTypeEntityList = feeSettingsDAO.getFundsTypeListByIds(entity.getFundsType());
            for (FundsTypeEntity q:fundsTypeEntityList) {
                fundsTypeName+=q.getName()+",";
            }
            entity.setFundsTypeName(fundsTypeName.substring(0,fundsTypeName.length()-1));
            List<MemberLVEntity> memberLVEntityList = feeSettingsDAO.selectToListByLV(entity.getMemberGradeId());
            for (MemberLVEntity q:memberLVEntityList) {
                LVName+=q.getMemberGradeName()+",";
            }
            entity.setMemberGradeName(LVName.substring(0,LVName.length()-1));
        }
        PageResultDTO<FeeSettingsVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, entityList, FeeSettingsVO.class);
        return ResponseDTO.succData(pageResultDTO);
    }

    /***
     * 保存
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<String> save(FeeSettingsAddDTO addDTO) {
        String memberGradeIdstrs = addDTO.getMemberGradeId();
        if(memberGradeIdstrs.indexOf(",")>0){
            String[] memberGradeIds = memberGradeIdstrs.split(",");
            String name = addDTO.getName();
            for (int i = 0; i < memberGradeIds.length; i++) {
                addDTO.setName(name+"LV"+memberGradeIds[i]);
                addDTO.setMemberGradeId(memberGradeIds[i]);
                FeeSettingsEntity entity = feeSettingsDAO.getByGName(addDTO);
                if (entity != null) {
                    return ResponseDTO.wrap(FeeSettingsCodeConst.ALREADY_EXIST);
                }
                feeSettingsDAO.insertSave(addDTO);
            }
        }else {
            addDTO.setName(addDTO.getName()+"LV"+addDTO.getMemberGradeId());
            FeeSettingsEntity entity = feeSettingsDAO.getByGName(addDTO);
            if (entity != null) {
                return ResponseDTO.wrap(FeeSettingsCodeConst.ALREADY_EXIST);
            }
            feeSettingsDAO.insertSave(addDTO);
        }

        return ResponseDTO.succ();
    }

    /**
     * 查询数据BYId
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<FeeSettingsVO> selectById(String id) {
        String areaName = " ";
        FeeSettingsEntity entity = feeSettingsDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(FeeSettingsCodeConst.NOT_EXIST);
        }
        List<AreaEntity> areaEntityList = registrationTemplateDAO.getAreaListByIds(entity.getAreaId());
        for (AreaEntity q:areaEntityList) {
            areaName+=q.getName()+",";
        }
        entity.setAreaName(areaName.substring(0,areaName.length()-1));

        FeeSettingsVO configDTO = SmartBeanUtil.copy(entity, FeeSettingsVO.class);
        List<MemberLVEntity> MemberLVEntityList = feeSettingsDAO.selectToLVListById(id);
        configDTO.setMemberLVEntityList(MemberLVEntityList);
        return ResponseDTO.succData(configDTO);
    }

    /**
     * 修改数据BYId
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<String> updateById(FeeSettingsUpdateDTO updateDTO) {
        FeeSettingsEntity entity = feeSettingsDAO.getById(updateDTO.getId());
        if (entity == null) {
            return ResponseDTO.wrap(FeeSettingsCodeConst.NOT_EXIST);
        }
        String memberGradeIdstrs = updateDTO.getMemberGradeId();
        if(memberGradeIdstrs.indexOf(",")>0){
            String[] memberGradeIds = memberGradeIdstrs.split(",");
            String name = updateDTO.getName();
            for (int i = 0; i < memberGradeIds.length; i++) {
                updateDTO.setName(name.substring(0,name.length()-1)+memberGradeIds[i]);
                updateDTO.setMemberGradeId(memberGradeIds[i]);
                RegistrationTemplateEntity alreadyEntity = feeSettingsDAO.getByKeyExcludeId(updateDTO);
                if (alreadyEntity != null) {
                    return ResponseDTO.wrap(FeeSettingsCodeConst.ALREADY_EXIST);
                }
                if(i==0){
                    feeSettingsDAO.updateById(updateDTO);
                }else {
                    feeSettingsDAO.insertSave(updateDTO);
                }

            }
        }else {
            feeSettingsDAO.updateById(updateDTO);
        }

        return ResponseDTO.succ();
    }

    /**
     * 删除数据BYId
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public ResponseDTO<String> deleteById(String id) {
        FeeSettingsEntity entity = feeSettingsDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(FeeSettingsCodeConst.NOT_EXIST);
        }
        feeSettingsDAO.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     *
     * @author
     * @date 2020/7/16 0016 17:54
     * @return com.java.test.dto.ResponseDTO<java.lang.String>
     */
    public ResponseDTO<List<FeeTypeVO>> selectToListByFeeType() {
        List<FeeTypeEntity> typeList = feeSettingsDAO.selectToListByFeeType();
        List<FeeTypeVO> voList = SmartBeanUtil.copyList(typeList,FeeTypeVO.class);
        return ResponseDTO.succData(voList);
    }

    /**
     * 查询未设置的会员等级
     * @author lijie1
     * @date 2020/10/10 0010 10:56
     * @return com.java.test.dto.ResponseDTO<java.util.List < com.java.test.system.feesettings.vo.MemberLVVO>>
     */
    public ResponseDTO<List<MemberLVVO>> selectToListByLV(String feeType) {
        List<MemberLVEntity> typeList = feeSettingsDAO.selectToListByLV();
        List<FeeSettingsEntity> feeSettingsList = feeSettingsDAO.selectToListByFeeT(feeType);
        for (FeeSettingsEntity obj:feeSettingsList) {
            String memberGradeIds = obj.getMemberGradeId();
            String[] memberGradeId = memberGradeIds.split(",");
            for (int i = 0; i < memberGradeId.length; i++) {
                for (int j = 0; j < typeList.size(); j++) {
                    MemberLVEntity ob = typeList.get(j);
                    if(Long.parseLong(memberGradeId[i])==(ob.getId())){
                        typeList.remove(j);
                    }
                }
            }
        }

        List<MemberLVVO> voList = SmartBeanUtil.copyList(typeList,MemberLVVO.class);
        return ResponseDTO.succData(voList);
    }


}
